package org.smart4j.framework.bean;

import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Wangminze on 2016/12/17.
 * Description: 请求参数对象
 */
public class Param {

    private List<FormParam> formParamList;
    
    private List<FileParam> fileParamList;
    
    public Param(List<FormParam> formParamList){
        this.formParamList = formParamList;
    }
    
    public Param(List<FormParam> formParamList,List<FileParam> fileParamList){
        this.formParamList = formParamList;
        this.fileParamList = fileParamList;
    }

    /**
     * @description : 获取请求参数映射
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @return : 
     **/
    public Map<String, Object> getFieldMap(){
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        if (CollectionUtil.isNotEmpty(formParamList)){
            for (FormParam formParam : formParamList){
                String fieldName = formParam.getFieldName();
                Object fieldValue = formParam.getFieldValue();
                if (fieldMap.containsKey(fieldName)){
                    fieldValue = fieldMap.get(fieldName) + StringUtil.SEPARATOR
                        + fieldValue;
                    
                }
                fieldMap.put(fieldName, fieldValue);
            }
        }
        return fieldMap;
    }

    /**
     * @description : 获取上传文件映射
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @return : 
     **/
    public Map<String, List<FileParam>> getFileMap(){
        Map<String, List<FileParam>> fileMap = new HashMap<String, List<FileParam>>();
        if (CollectionUtil.isNotEmpty(fileParamList)){
            for (FileParam fileParam : fileParamList){
                String fieldName = fileParam.getFieldName();
                List<FileParam> fileParamList;
                if (fileMap.containsKey(fieldName)){
                    fileParamList = fileMap.get(fieldName);
                }else{
                    fileParamList = new ArrayList<FileParam>();
                }
                fileParamList.add(fileParam);
                fileMap.put(fieldName, fileParamList);
            }
        }
        return fileMap;
    }
    
    /**
     * Description : 获取所有上传文件
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName 表单字段名
     * @return 文件清单
     **/
    public List<FileParam> getFileList(String fieldName){
        return getFileMap().get(fieldName);
    }

    /**
     * Description : 获取唯一上传文件
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName 表单字段名
     * @return 单个文件
     **/
    public FileParam getFile(String fieldName){
        List<FileParam> fileParamList = getFileList(fieldName);
        if (CollectionUtil.isNotEmpty(fileParamList) && fileParamList.size() == 1){
            return fileParamList.get(0);
        }
        return null;
    }

    /**
     * Description : 验证参数是否为空
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @return : true为空，false为非空
     **/
    public boolean isEmpty(){
        return CollectionUtil.isEmpty(formParamList)
                && CollectionUtil.isEmpty(fileParamList);
    }

    /**
     * Description : 根据参数名获取String型参数值
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName
     * @return 当字段中没有该字段名时返回NULL
     **/
    public String getString(String fieldName){
        return CastUtil.cast2String(getFieldMap().get(fieldName),null);
    }

    /**
     * Description : 根据参数名获取double型参数值
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName
     * @return 当字段中没有该字段名或该字段类型不匹配时返回Double的最小值
     */
    public double getDouble(String fieldName){
        return CastUtil.cast2Double(getFieldMap().get(fieldName),Double.MIN_VALUE);
    }

    /**
     * Description : 根据参数名获取long型参数值
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName
     * @return 当字段中没有该字段名或该字段类型不匹配时返回long的最小值
     */
    public long getLong(String fieldName){
        return CastUtil.cast2Long(getFieldMap().get(fieldName),Long.MIN_VALUE);
    }

    /**
     * Description : 根据参数名获取int型参数值
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName
     * @return 当字段中没有该字段名或该字段类型不匹配时返回Integer的最小值
     */
    public int getInt(String fieldName){
        return CastUtil.cast2Int(getFieldMap().get(fieldName),Integer.MIN_VALUE);
    }

    /**
     * Description : 根据参数名获取boolean型参数值
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param fieldName
     * @return 当字段中没有该字段名或该字段类型不匹配时返回Boolean.FALSE
     */
    public boolean getBoolean(String fieldName){
        return CastUtil.cast2Boolean(getFieldMap().get(fieldName),Boolean.FALSE);
    }

}
